package com.qdu.servlet;

import com.qdu.entity.Admin;
import com.qdu.entity.User;
import com.qdu.util.DatabaseUtil;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 该Servlet负责处理修改学生信息的请求
 *
 * @author Anna
 */
@WebServlet("/a_mss")
public class Admin_ModifyServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取学生修改后的信息，封装成一个学生对象，因为本程序不让修改班级和性别，也没提交班级和性别，所以就不获取了
        req.setCharacterEncoding("utf-8");
        String srollno = req.getParameter("srollno");
        String spwd = req.getParameter("spassword");
        String sname = req.getParameter("sname");

        //获取学生的3项信息，封装成一个Student对象
        User newUser = new User(null, req.getParameter("Uname"), req.getParameter("Upwd"), req.getParameter("Uemail"));
        Admin newAdmin = new Admin(req.getParameter("Uname"), req.getParameter("Upwd"));
        //2. 执行业务逻辑
        //修改学生姓名和性别
//        String driver_class = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
//        String url = "jdbc:sqlserver://localhost:1433;databaseName=Crp";
//        String username = "sa";
//        String password = "sa";

        Connection con = null;
        PreparedStatement ps = null;
        int rows = 0; //受影响的行数

        try {
//            Class.forName(driver_class);
//            con = DriverManager.getConnection(url, username, password);
con = DatabaseUtil.getConnection();
            if (req.getParameter("optins").equals("Admin")) {
                ps = con.prepareStatement("UPDATE [dbo].[Admin] set [Aname]=?, [Apwd]=? WHERE [Aname]=?");
                ps.setString(1, newAdmin.getAneme());
                ps.setString(2, newAdmin.getApwd());
                ps.setString(3, newAdmin.getAneme());
                rows = ps.executeUpdate();
            } else {
 ps = con.prepareStatement("UPDATE [dbo].[User] set [Uname]=?, [Upwd]=?,[Uemail]=? WHERE [Uname]=?");
                ps.setString(1, newUser.getUname());
                ps.setString(2, newUser.getUpwd());
                ps.setString(3, newUser.getUemail());
                ps.setString(4, newUser.getUname());
                rows = ps.executeUpdate();
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != ps) {
                    ps.close();
                }
                if (null != con) {
                    con.close();
                }

            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }

        //3. 生成响应内容，这里返回受影响的行数用于判断是否修改成功
        resp.getWriter().print(rows);

    }
}
